#include "devices.h"
#include "highspeed/highspeed.h"
#include <fstream>
#include <string>

using namespace std;

#define OUT_INHIBIT(is, func) \
if (is) \
    freopen("/dev/null", "w", stdout); \
func; \
if (is) \
    freopen("/dev/tty", "w", stdout);

static bool neat = true;

int main(int argc, char *argv[])
{
    int n;
    char *buff;

    for (int i = 1; i < argc; ++i) {
        if ("--verbose" == string(argv[i]))
            neat = false;

        if ("-v" == string(argv[i]))
            neat = false;
    }

    n = highspeed::getStatus();
    cout << "Status : " << highspeed::statuses[n] << endl;

    n = highspeed::init();
    cout << endl << "Initialization : " << (n ? "failed" : "ok") << endl;

    n = highspeed::getStatus();
    cout << "Status : " << highspeed::statuses[n] << endl;

    const char *path = DEFAULT_OUTPUT_IMAGE;
    OUT_INHIBIT(neat, buff = highspeed::takePicture());
    cout << "Take picture :" << path << endl;

    ofstream out("/tmp/photo.jpg.b64", ios::out);

    if (out.is_open()) {
        out << buff << endl;
        out.close();
    }

    delete buff;

    OUT_INHIBIT(neat, n = highspeed::setColorStyle(2));
    cout << "Set style of color : " << (n ? "failed" : "ok") << endl;

    path = "/tmp/photo_bw.jpg";
    OUT_INHIBIT(neat, buff = highspeed::takePicture(path));
    cout << "Take picture : " << path << endl;
    delete buff;

    OUT_INHIBIT(neat, highspeed::setColorStyle(0));

    OUT_INHIBIT(neat, n = highspeed::rotate(3));
    cout << "rotate: " << (n ? "failed" : "ok") << endl;

    path = "/tmp/photo_rotate.jpg";
    OUT_INHIBIT(neat, buff = highspeed::takePicture(path));
    cout << "Take picture : " << path << endl;
    delete buff;

    OUT_INHIBIT(neat, n = highspeed::close());
    cout << "Close camera : " << (n ? "failed" : "ok") << endl;

    n = highspeed::getStatus();
    cout << "Status : " << highspeed::statuses[n] << endl;

    return 0;
}
